// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-11 15:36:29
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2025-01-02 17:12:28
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Constraint\RBARParser.cpp
 */

#include "Import/BDFImport/BulkData/Constraint/RBARParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <set>
#include <unordered_set>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IRBARService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (RBAR);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::Import::BDF;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/RBAR/RBAR.md>
*/

/**
 * @brief 生成补集字符串.
 *
 * 此函数接收一个输入字符串，检查目标集合 {1, 2, 3, 4, 5, 6} 中缺少的字符，
 * 并返回由这些缺少字符组成的字符串。如果没有缺少的字符，则返回空字符串。
 *
 * @param inputStr 输入的原始字符串。
 * @return std::string 返回由缺少字符组成的新字符串，如果没有缺少的字符则返回空字符串。
 */
SG::DataStructure::Common::I64 creatComplementarySet (SG::DataStructure::Common::I64 oriNum)
{
    // 目标数字集合
    std::set<char> targetDigits = { '1', '2', '3', '4', '5', '6' };
    // 转换输入数字为字符串
    std::string const aStr = std::to_string (oriNum);

    // 遍历输入数字，将其中存在的目标数字移除
    for (char const c : aStr)
    {
        targetDigits.erase (c);
    }

    // 将剩余的目标数字组合成新数字
    std::string result;
    for (char const c : targetDigits)
    {
        result += c;
    }
    if (result.empty ())
    {
        return 0;
    }

    return convertToI64 (result);
}

enum RBARKey : std::uint8_t
{
    RBARKey_EID = 2,
    RBARKey_GA  = 3,  // Grid point identification number of connection points.
    RBARKey_GB  = 4,
    RBARKey_CNA = 5,  // Component numbers of independent degrees-of-freedom in the global coordinate system for the element at grid points
                      // GA and GB. See Remark 3. (Integers 1 through 6 with no embedded blanks)
    RBARKey_CNB = 6,
    RBARKey_CMA = 7,  // Component numbers of dependent degrees-of-freedom in the global coordinate system assigned by the element at grid
                      // points GA and GB. See Remarks 4. and 5. (Integers 1 through 6 with no embedded blanks, or zero or blank.)
    RBARKey_CMB   = 8,
    RBARKey_ALPHA = 9,  // Thermal expansion coefficient. See Remark  11. (Real or blank)
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseRBAR (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::RBARData tempData;
    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    // 解析字符串

    int curIDKey = 1;  // 当前字符串是第几项

    for (auto curStrKey : dividedEntryInfo)
    {
        // 空字符串跳过，并将curIDKey  +1
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }
        // 非空提前转换科学计数法，避免字符串->数字转换失败

        switch (curIDKey)
        {
        case RBARKey_EID:
            tempData.m_id = convertToI64 (curStrKey);
            break;
        case RBARKey_GA:
            tempData.m_node1 = convertToI64 (curStrKey);
            break;
        case RBARKey_GB:
            tempData.m_node2 = convertToI64 (curStrKey);
            break;
        case RBARKey_CNA:
            tempData.m_masterDof1 = convertToI64 (curStrKey);
            break;
        case RBARKey_CNB:
            tempData.m_masterDof2 = convertToI64 (curStrKey);
            break;
        case RBARKey_CMA:
            tempData.m_slaveDof1 = convertToI64 (curStrKey);
            break;
        case RBARKey_CMB:
            tempData.m_slaveDof2 = convertToI64 (curStrKey);
            break;
        case RBARKey_ALPHA:
            tempData.m_alpha = convertToReal (curStrKey);
            break;
        default:
            break;
        }
        curIDKey++;
    }

    // 2024/06/25
    // 导入数据默认值取值规则修改:
    // 1. 如果 CNA 和 CNB均为空， CNA = 123456， CNB = NULL;
    if (tempData.m_masterDof1 == 0 && tempData.m_masterDof2 == 0)
    {
        tempData.m_masterDof1 = 123456;
    }

    // 2. 如果 CMA 和 CMB均为空， CMA 取值为CNA的补集， CMB 取值为 CNB 补集
    if (tempData.m_slaveDof1 == 0 && tempData.m_slaveDof2 == 0)
    {
        tempData.m_slaveDof1 = creatComplementarySet (tempData.m_masterDof1);
        tempData.m_slaveDof2 = creatComplementarySet (tempData.m_masterDof2);
    }

    return dbServiceFactory->get<IRBARService> ()->Add (tempData);
}
